package com.dpteam.controller;

import com.dpteam.bean.MemberInfo;
import com.dpteam.service.MemberService;
import com.dpteam.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: luolei
 * Date: 2016/1/13
 * Time: 9:07
 */
@Controller
@RequestMapping("member")
public class MemberController {

    @Resource
    private MemberService memberService;

    private static Logger logger = LoggerFactory.getLogger(MemberController.class);

    @RequestMapping("memberSearch")
    public String memberSearch(String phone, ModelMap map) {
        logger.info("memberSearch [phone:{}]", phone);
        // 校验
        boolean check = InputCheckUtil.isPhone(phone);
        if (!check) {
            // 校验失败，重新刷新界面
            logger.info("校验失败");
            return "forward:/link/memberSearch";
        }

        List<MemberInfo> memberInfos = memberService.find(phone);
        map.put(Constant.members, memberInfos);
        Page page = PageFactory.getPage();
        page.count(1);
        map.put(Constant.page, page);
        return "memberSearch";
    }

    @RequestMapping("memberAdd")
    public String memberAdd(String memberName, int memberSex, String IDCardNo, String memberPhoneNumber, int userId) {
        logger.info("添加会员[memberName:{},memberSex:{},IDCardNo:{},memberPhoneNumber:{},userId:{}]", memberName, memberSex, IDCardNo, memberPhoneNumber, userId);
        // 校验
        boolean check = StringUtil.isNotEmpty(memberName)
                && InputCheckUtil.isIDCartNo(IDCardNo)
                && InputCheckUtil.isPhone(memberPhoneNumber);
        if (!check) {
            // 校验失败，重新刷新界面
            logger.info("校验失败");
            return "forward:/link/memberAdd";
        }

        boolean result = memberService.add(memberName, memberSex, IDCardNo, memberPhoneNumber, userId);
        if (result) {
            //添加成功
            return "forward:/link/memberSearch";
        } else {
            //添加失败
            return "error";
        }
    }
}
